class Solution(object):
    def numSubarrayBoundedMax(self, nums, left, right):
        i = 0
        j = 0
        n = len(nums)
        c = 0
        t = 0
        while j < n:
            if nums[j] > right:
                c = -1
                i = j + 1
            else:
                if left <= nums[j] <= right:
                    t += (j - i + 1)
                    c = j
                else:
                    if c >= 0:
                        t += (c - i + 1)
            j += 1
        return t
